package com.neusoft.rental.service.impl;

import com.neusoft.rental.mapper.InformationMapper;
import com.neusoft.rental.mapper.PlaceMapper;
import com.neusoft.rental.mapper.PlaceappointmentMapper;
import com.neusoft.rental.po.*;
import com.neusoft.rental.service.GoodsAppointmentService;
import com.neusoft.rental.service.PlaceappointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PlaceappointmentServiceImpl implements PlaceappointmentService {

    @Autowired
    private PlaceappointmentMapper placeappointmentMapper;
    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private PlaceMapper placeMapper;


    //通过与当前时间比较获取还没到归还时间的已预约的场地
    @Override
    public List<Venuetime> querytimeperiod(Placeappointment placeappointment){
        //Venuetime类是一个专门存已被预约的时间段开始时间和结束时间的类
        List<Venuetime> venuetimeList=new ArrayList<Venuetime>();
        List<Placeappointment> placeappointmentList=
                placeappointmentMapper.queryappointment(placeappointment);

        Date date=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datenow=formatter.format(date);
        //通过循环遍历与当前时间作比较，看结束时间是否在开始时间之后，如果在，加入已被预约时间段列表
        for(Placeappointment p:placeappointmentList){
            if(datenow.compareTo(p.getEndtime())<0){
                Venuetime venuetime=new Venuetime(p.getStarttime(),p.getEndtime());
                venuetimeList.add(venuetime);
            }
        }
        return venuetimeList;
    }


    //预约场地接口，同时通过提前与已预约的时间段进行比较，在系统前台可以先进行避免冲突，后台可直接审核
    @Override
    public int orderplace(Placeappointment placeappointment) {
        Placeappointment placeappointment1=new Placeappointment();
        placeappointment1.setPlaceid(placeappointment.getPlaceid());
        placeappointment1.setJudge(2);
        //调用上面的获取方法获取已预约的时间段
        List<Venuetime> venuetimeList=querytimeperiod(placeappointment);
        String endTime=placeappointment.getEndtime();
        String startTime=placeappointment.getStarttime();
        for(Venuetime v:venuetimeList){
            //判断是否冲突
            if(startTime.compareTo(v.getStime())>=0&&startTime.compareTo(v.getEtime())<0
                ||endTime.compareTo(v.getStime())>0&&endTime.compareTo(v.getEtime())<=0){
                return 0;
            }
        }
        return placeappointmentMapper.orderplace(placeappointment);
    }


    //利用judge查找预约信息，judge为0,是需要院级审核的场地信息，judge为1是校级需要审核的场地预约信息
    //judge=2为用户查找已经通过申请的场地
    @Override
    public List<Placeappointment> queryappointment(Placeappointment placeappointment) {
        //管理员查询场地申请
        if(placeappointment.getJudge()!=null){
            Date date=new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datenow=formatter.format(date);
            List<Placeappointment> pList=placeappointmentMapper.queryappointment(placeappointment);
            List<Placeappointment> pList1=new ArrayList<Placeappointment>();

            for(Placeappointment p:pList){
                if(datenow.compareTo(p.getStarttime())>0 && p.getJudge()!=2){
                    Place place=placeMapper.queryPlacebyid(p.getPlaceid());
                    Information information=new Information(null,p.getUserid(),
                            "系统提醒",place.getPlacename()+"预约失败," +
                            "管理员超时未处理，请另选时间预约",datenow,0);
                    informationMapper.addInformation(information);
                    placeappointmentMapper.deletePlaceappointment(p);
                }
                else{
                    pList1.add(p);
                }
            }
            return pList1;
        }
        //用户查询场地申请
        else{
            return placeappointmentMapper.queryappointment(placeappointment);
        }
    }


    //用户归还场地，和管理员审核场地
    //更新对应预约信息中的judge,judge为1表示院级审核同意预约信息，judge为2表示校级同意，审核完成同时给用户发送消息
    //审核完成同时改变对应场地的预约人数，审核不通过删除申请信息。
    @Override
    public int judgePlaceappoinment(Placeappointment placeappointment) {
        //管理员审核场地申请
        if(placeappointment.getJudge()!=null){
            //通过placeid查找对应place，短信发送需要用到placename
            Place place=placeMapper.queryPlacebyid(placeappointment.getPlaceid());
            Date date=new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datenow=formatter.format(date);

            Information information1=new Information(null,placeappointment.getUserid(),
                    "系统提醒",place.getPlacename()+"通过审核，预约成功",datenow,0);
            Information information2=new Information(null,placeappointment.getUserid(),
                    "系统提醒",place.getPlacename()+"预约失败,请另选场地或时间预约",datenow,0);

            //如果校级领导同意，那么场地预约成功，发送系统消息且更新place表中的ordernum预约人数
            if(placeappointment.getJudge()==2){
                Place place1=new Place();
                //通过设置ordernum为1，若为1则ordernum更新为ordernum+1，ordernum为null不更新
                place1.setOrdernum(1);
                place1.setPlaceid(placeappointment.getPlaceid());
                place1.setPlacename(place.getPlacename());
                place1.setPlacepho(place.getPlacepho());
                place1.setPush(place.getPush());
                place1.setPlacetype(place.getPlacetype());
                placeMapper.updatePlace(place1);
                informationMapper.addInformation(information1);
                return placeappointmentMapper.judgePlaceappoinment(placeappointment);
            }
            else if(placeappointment.getJudge()==1){
                System.out.println(placeappointment);
                System.out.println(placeappointmentMapper.judgePlaceappoinment(placeappointment));
                return placeappointmentMapper.judgePlaceappoinment(placeappointment);
            }
            else{
                //如果院级领导或者校级领导审核不通过，judge为0，则发送预约失败的消息，并删除该预约记录。
                informationMapper.addInformation(information2);
                return placeappointmentMapper.deletePlaceappointment(placeappointment);
            }
        }
        //用户归还场地
        else{
            Place place=placeMapper.queryPlacebyid(placeappointment.getPlaceid());
            Place place1=new Place();
            place1.setPlaceid(placeappointment.getPlaceid());
            place1.setOrdernum(-1);
            place1.setPlacename(place.getPlacename());
            placeMapper.updatePlace(place1);
            return placeappointmentMapper.judgePlaceappoinment(placeappointment);
        }
    }

    @Autowired
    private GoodsAppointmentService goodsappointmentService;

    //返回给用户所有的预约成功的场地与物资
    public List<Object> queryallappointment(String userid){
        Placeappointment p=new Placeappointment();
        p.setUserid(userid);

        Goodsappointment g=new Goodsappointment();
        p.setUserid(userid);

        List<Object> gAndPList=new ArrayList<Object>();
        List<Placeappointment> pList=queryappointment(p);
        List<Goodsappointment> gList=goodsappointmentService.getGoodsappointment(g);
        gAndPList.add(pList);
        gAndPList.add(gList);
        return gAndPList;
    }

}
